class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> v;
        stack<TreeNode*> st;
        TreeNode* cur = root;

        while (cur || !st.empty())
        {

            while (cur)
            {
                st.push(cur);
                v.push_back(cur->val);
                cur = cur->left;
            }

            TreeNode* temp = st.top();
            st.pop();

            cur = temp->right;
        }

        return v;
    }
};